package com.whpost.service.zhdt.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.t.TXzjg;
import com.whpost.service.entity.t.TreeNode;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.BaseServices;
import com.whpost.service.service.t.TRegionServices;
import com.whpost.service.service.t.TXzjgServices;
import com.whpost.service.zhdt.dao.ZhdtWgfzMapper;
import com.whpost.service.zhdt.entity.ZhdtWgfz;
import com.whpost.service.zhdt.entity.ZhdtWgxx;
import com.whpost.service.zhdt.entity.ZhdtZbfl;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZhdtMainServices extends BaseServices<ZhdtWgfzMapper, ZhdtWgfz> {

    @Resource
    private TRegionServices tRegionServices;
    @Resource
    private ZhdtWgfzServices zhdtWgfzServices;
    @Resource
    private TXzjgServices tXzjgServices;
    @Resource
    private ZhdtWgxxServices zhdtWgxxServices;
    @Resource
    private ZhdtZbflServices zhdtZbflServices;

    //数字网格初始数据
    public ResultInfo initMain(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            TCzy tCzy = gettCzyDao().selectById(loginId);
            //返回前端的内容
            Map<String,Object> mainData = new HashMap<>();

            List<TreeNode> tXzjgTree = tXzjgServices.getTreeSelect(null);
            mainData.put("tXzjgTree",tXzjgTree);

            List<TreeNode> tRegionTree = tRegionServices.getTreeSelect(null);
            mainData.put("tRegionTree",tRegionTree);

            List<TreeNode> zhdtWgfzTree = zhdtWgfzServices.getTreeSelect(null);
            mainData.put("zhdtWgfzTree",zhdtWgfzTree);

            List<ZhdtWgxx> zhdtWgxxList = zhdtWgxxServices.lambdaQuery().eq(ZhdtWgxx::getState,"1").list();
            mainData.put("zhdtWgxxList",zhdtWgxxList);
            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",mainData);
            return backRtn(resultInfo,loginId);
        }
    }

    //网点标注基础数据
    public ResultInfo initWdbz(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            TCzy tCzy = gettCzyDao().selectById(loginId);
            //返回前端的内容
            Map<String,Object> rtnMap = new HashMap<>();

            String treejson = tXzjgServices.getTree();
            rtnMap.put("treejson", JSON.parse("[" + treejson + "]"));

            List<ZhdtZbfl> zhdtZbflList = zhdtZbflServices.lambdaQuery().eq(ZhdtZbfl::getState,"1").list();
            rtnMap.put("zhdtZbflList",zhdtZbflList);
            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",rtnMap);
            return backRtn(resultInfo,loginId);
        }
    }

    //保存区边界文件
    public ResultInfo saveQu(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            TCzy tCzy = gettCzyDao().getCzyById(loginId);

            String xzjg_id = "";
            if (jsonObject.containsKey("xzjg_id")) {
                xzjg_id = jsonObject.getString("xzjg_id");
            }
            ResultInfo resultInfo = null;
            TXzjg tXzjg = tXzjgServices.getById(xzjg_id);
            if (tXzjg!=null){
                if ("1".equals(tXzjg.getSfqu())){
                    try {
                        String filename = tXzjg.getZjm().toLowerCase()+".json";
                        DistrictJsonSaver.saveDistrictJson(tXzjg.getName(),"武汉市",filename);
                        tXzjg.setWglj(filename);
                        tXzjgServices.updateById(tXzjg);
                        resultInfo = new ResultInfo(0, "更新成功！",null);
                    } catch (IOException e) {
                        throw new RuntimeException(tXzjg.getName()+"获取边界失败！");
                    }
                }else{
                    resultInfo = new ResultInfo(500, "只有级别是区才允许获取边界！",null);
                }
            }else{
                resultInfo = new ResultInfo(500, "机构或者区域信息不存在！",null);

            }
            return backRtn(resultInfo,loginId);

        }
    }
}
